﻿using UnityEngine;
using System.Collections;
using System;



public class LevelManager : Singleton<LevelManager> {
    public GameObject panelAgentCenter;
    public GameObject panelAgentLeft;
    public GameObject panelAgentRight;
    public GameObject panelLevel;
    public GameObject levelAllLavel;


    [HideInInspector]
    private LevelDatabase levelDatabase;

    public LevelDatabase LevelDatabase
    {
        get { return levelDatabase; }
        set { levelDatabase = value; }
    }

    private int _iNumLevelPerPage;

    public int iNumLevelPerPage
    {
        get { return _iNumLevelPerPage; }
        set { _iNumLevelPerPage = value; }
    }
    private int _iCurrentLevel;

    public int iCurrentLevel
    {
        get { return _iCurrentLevel; }
        set { _iCurrentLevel = value; }
    }
    public LevelManager()
    {
        iNumLevelPerPage = 3; // 3 button per page
        iCurrentLevel = 1; // set mac dinh
    }

    [HideInInspector]
    public string currButtonTag;

    [HideInInspector]
    public int currentPage;

	void Start () {
        
       ReadDatabase();
       ToUI();
        
    }

    void ReadDatabase()
    {
        levelDatabase = LevelDatabase.Instantiate();
        levelDatabase.ReadDatabase();
    }
    public void ToUI()
    {
        
        double temp = (double)levelDatabase.Waves.Length/(double)iNumLevelPerPage;

        int maxLevelPage = temp % Math.Round(temp) == 0 ? (int)temp : (int)temp + 1;
        int maxWaves = levelDatabase.Waves.Length;

        temp = (double)iCurrentLevel/(double)iNumLevelPerPage;

        currentPage = temp % Math.Round(temp) == 0 ? (int)temp : (int)temp + 1;
        //Debug.Log(levelAllLavel);
        levelAllLavel.GetComponent<UILabel>().text = currentPage.ToString() + "/" + maxLevelPage.ToString();

   
        panelAgentCenter.GetComponent<UITexture>().mainTexture = levelDatabase.DataTexture.ListImageAgent[iCurrentLevel - 1];
        panelAgentRight.GetComponent<UITexture>().mainTexture = iCurrentLevel >= maxWaves ? null : levelDatabase.DataTexture.ListImageAgent[iCurrentLevel];
        panelAgentLeft.GetComponent<UITexture>().mainTexture = iCurrentLevel == 1 ? null : levelDatabase.DataTexture.ListImageAgent[iCurrentLevel - 2];

        

        UpdateButton();
        
    }

    void UpdateButton()
    {
        bool ActiveButton4 = true;
        int maxCurrentLevel = currentPage * 3;
        if(maxCurrentLevel  > levelDatabase.Waves.Length)
        {
            LevelButtonContainer.Instance.Button3.SetActive(false);
            ActiveButton4 = false;
        }
        else
        {
            LevelButtonContainer.Instance.Button3.SetActive(true);
            LevelButtonContainer.Instance.Button3.GetComponentInChildren<UILabel>().text = maxCurrentLevel.ToString();
            ActiveButton4 = true;
            Debug.Log("Set Active OK");
        }

        if (maxCurrentLevel - 1 > levelDatabase.Waves.Length)
        {
            LevelButtonContainer.Instance.Button2.SetActive(false);
            ActiveButton4 = false;
        }
        else
        {
            LevelButtonContainer.Instance.Button2.SetActive(true);
            LevelButtonContainer.Instance.Button2.GetComponentInChildren<UILabel>().text = (maxCurrentLevel - 1 ).ToString();
            
            ActiveButton4 = true;
        }

        if (maxCurrentLevel - 2 > levelDatabase.Waves.Length)
        {
            LevelButtonContainer.Instance.Button1.SetActive(false);
            ActiveButton4 = false;
        }
        else
        {
            LevelButtonContainer.Instance.Button1.SetActive(true);
            LevelButtonContainer.Instance.Button1.GetComponentInChildren<UILabel>().text = (maxCurrentLevel - 2).ToString();
            
        }

        LevelButtonContainer.Instance.Button4.SetActive(ActiveButton4);
    }
    

    IEnumerable MovePanelAgent()
    {
        while (true)
        {
            yield return MovePanelAgent();
        }
    }
}
 